package com.lzp.controller;

import com.lzp.entity.User;
import com.lzp.service.UserService;
import com.lzp.utils.Constants;
import com.lzp.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by liuzp on 2017/11/23.
 */
@RestController
@RequestMapping("/user")
public class UserCtrl {

    @Autowired
    private UserService userService;

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public User get(@PathVariable String id) {
        return userService.get(id);
    }

    /**
     * 根据主键删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable String id) {
        return userService.remove(id)  ? Constants.SUCCESS : Constants.FAIL;
    }

    /**
     * 列表查询带分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Page list(Integer pageNum,Integer pageSize) {
        Page page  = userService.list(pageNum,pageSize);
        return  page;
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.PUT)
    public String update(@RequestBody User user){
        return userService.update(user) ? Constants.SUCCESS : Constants.FAIL;
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public String save(@RequestBody User user){
        userService.insert(user);
        return  Constants.SUCCESS;
    }


}
